﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div id="tabs">
        <ul>
         
            <li><a href="#tabs-2">Master Cart</a></li>
        </ul>
        <div id="tabs-1" style="min-height: 500px; margin-left: 0px;">
            <table id="cart" style="display: none">
            </table>
        </div>
        <div id="tabs-2">
            <table id="tbl" style="display: none">
            </table>
        </div>
    </div>
    <div id="dialog-add"  title="Add Cart">
        <table>
            <tr>
                <td>
                    Cart No. :
                </td>
                <td>
                    <input type="text" id="cartno" maxlength="3"  />
                </td>
            </tr>
            <tr>
                <td>
                    Cart Type :
                </td>
                <td>
                    <input type="text" id="txtType" maxlength="3" />
                </td>
            </tr>
             <tr>
                <td>
                    Status :
                </td>
                <td>
                    <input type="checkbox" id="chkNot"  />Not Use
                </td>
            </tr>
        </table>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="FeaturedContent" runat="server">
    <% if (false)
   { %>
<script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<% } %>
</asp:Content>

<asp:Content ID="Content4" ContentPlaceHolderID="ScriptsSection" runat="server">

    <script type="text/jscript">
        $(function () {
            $("#tabs").tabs();
            $("#cartno").autoNumeric({ vMin: '0', vMax: '999', aPad: false });

            $("#dialog-add").dialog({
                width: 350,
                height: 250,
                autoOpen: false, position: [500, 150],
                open: function (event, ui) {
                    if ($("#cartno").is("[disabled]")) {
                        setTimeout(function () { $("#txtType").focus(); }, 1000);
                    } else {
                        setTimeout(function () { $("#cartno").focus(); }, 1000);
                    }

                },
                show: "blind",
                buttons: {
                    "บันทึก": function () {
                        //  $().toastmessage('showSuccessToast', 'บันทึกสำเร็จ');
                        if ($("#cartno").val() == "") {
                            $().toastmessage('showErrorToast', 'Pleas Input Cart No.');
                            setTimeout(function () { $("#cartno").focus(); }, 1000);
                            return;
                        }
                        if ($("#txtType").val() == "") {
                            $().toastmessage('showErrorToast', 'Pleas Input Cart Type');
                            $("#txtType").focus();
                            return;
                        }

                        var dataForSubmit = new Array();
                        dataForSubmit.push({
                            _Code: $("#cartno").val(),
                            _Type: $("#txtType").val(),
                            _NotUse: ($('#chkNot:checked').length > 0) ? "X" : ""
                        });

                        var getUsersUrl = '<%= Url.Action("add_Cart") %>';
                        if ($("#cartno").is("[disabled]")) {
                            getUsersUrl = '<%= Url.Action("Edit_Cart") %>';
                        }
                        callScriptMethod(getUsersUrl, { theData: dataForSubmit });


                        //   $("#cases").flexgridReload(newUrl);
                    },
                    "ยกเลิก": function () {
                        $("#dialog-add").dialog("close");
                    }
                }
            });

            $("#cart").flexigrid({
                url: '<%= Url.Action("CartList") %>',
                dataType: 'json',
                colModel: [
				{ display: 'Code', name: '_Code', width: 100, sortable: true, align: 'left' },
				{ display: 'Type', name: '_Type', width: 100, sortable: true, align: 'left' },
				{ display: 'NotUse', name: '_NotUse', width: 100, sortable: true, align: 'center' },
				{ display: 'ModifyBy', name: '_ModifyBy', width: 150, sortable: true, align: 'left' },
				{ display: 'Modify Datetime', name: '_ModifyDT', width: 180, sortable: true, align: 'left' }
				],
                buttons: [
                { name: 'Add', bclass: 'add', onpress: Adddata },
                   { separator: true },
                { name: 'Edit', bclass: 'edit', onpress: Editdata }

                ],
                sortname: '_Code',
                sortorder: 'asc',
                usepager: true,
                rp: 15,
                width: 900,
                height: 380, singleSelect: true
            });
            function Adddata() {
                $("#cartno").val("").removeAttr("disabled").focus();
                $("#txtType").val("");
                $("#chkNot").removeAttr("checked");
                $("#dialog-add").dialog("open");
            }
            function Editdata() {
                var row = ($("#cart").find(".trSelected").length);
                if (row <= 0) {
                    $().toastmessage('showErrorToast', "Please Select Data Edit");
                    return;
                }
                row = $("#cart .trSelected");
                var code = row.find("td:first").text();
                var types = row.find("td").eq(1).text();
                var notuse = row.find("td").eq(2).text().length;
                $("#cartno").val(code).attr("disabled", "disabled");
                $("#txtType").val(types);
                $("#chkNot").removeAttr("checked");

                if (row.find("td").eq(2).text() == "X") {
                    $("#chkNot").attr("checked", "checked");
                }
                $("#dialog-add").dialog("open");
            }
            function callScriptMethod(url, jsonObject, action, callback, async) {

                callback = callback || function () { };
                async = (async == null || async);

                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: url,
                    data: JSON.stringify(jsonObject),
                    dataType: "json",
                    async: async,
                    success: function (data) {

                        if (data.success == "1") {
                            $().toastmessage('showSuccessToast', data.message);
                            $("#cart").flexgridReload('<%= Url.Action("CartList") %>')
                            $("#dialog-add").dialog("close");
                        } else {
                            $().toastmessage('showErrorToast', data.message);

                        }
                    },
                    error: function (xhr, status, error) {
                        var err = xhr.responseText;
                        alert(err.Message);
                    }
                });
            }

        });

    </script>
</asp:Content>
